(function(){
	'use strict';
	angular.module('controllers.omdisplay.businessOperation')
	.controller('accuracyController',accuracyController);
	function accuracyController($scope,operationAccuracyService,$filter,COREDATA,$timeout,CoreLoading,CoreService){
		$scope.shoumun = 1;
		$scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
		var upayData =[
			{code:'1',name:'省公司'},
			{code:'2',name:'银行'}
		];
		
		var pbossData = [
			{code:'1',name:'省公司'},
			{code:'2',name:'HLR'},
			{code:'3',name:'运管平台'},
			{code:'4',name:'内容计费'},
			{code:'5',name:'业务网关'}
		];
		$scope.timeTypes = [
	    {tiemTypeCode:'1',timeTypeName:'天'},
			{timeTypeCode:'0',timeTypeName:'小时'}
		];
		$scope.upData = upayData;
		$scope.staticTypes = [
			{staticTypeCode:'1',staticTypeName:'对比'},
			{staticTypeCode:'2',staticTypeName:'趋势'},
			{staticTypeCode:'3',staticTypeName:'异动'}
		];
		$scope.sysNames = [
			{sysCode:'01',sysName:'统一支付'},
			{sysCode:'02',sysName:'物联网'}
		];

		$scope.translate = {
			sysCode:{sysCode:'01',sysName:'统一支付'},
			code:[],
			staticTypeCode: {staticTypeCode:'1',staticTypeName:'对比'},
			timeTypeCode: {timeTypeCode: '1',timeTypeName: '日'},
			startTime: new Date(),
			endTime: new Date()
		};
		$scope.clickTranslate = function(){
			$scope.translate.code = [];
			if($scope.translate.sysCode.sysCode === '01'){
				$scope.upData = pbossData;
			}else{
				$scope.upData = upayData;
			}
		};

		/**
		 * [ 设置账期起始时间的默认值]
		 * 初始化页面值的默认 查询账期为日
		 * @param {[type]} timeTypeCode [description]
		 */

		$scope.$watch('translate.timeTypeCode',function(newValue){
			var month = new Date().getMonth();                  //当前月份
			var day = new Date().getDate();                     //当前天
			var year = new Date().getFullYear();                //当前年份
			var hours = new Date().getHours();
			switch (newValue.timeTypeCode) {
				case '1':
					$scope.translate.startTime = new Date(year, month, day - 7);
					$scope.translate.endTime = new Date(year, month, day - 1);
					break;
				case '0':
					$scope.translate.startTime = new Date(year,month,day-7,hours);
					$scope.translate.endTime = new Date(year,month,day-1,hours);
			}
		});
        var myEchars =echarts.init(document.getElementById('traffic1')) ;
		var startTime;
		var endTime;
		$scope.queryBusinessAccuracy = function(){
			startTime = $filter('date')($scope.translate.startTime,'yyyy-MM-dd');
			endTime = $filter('date')($scope.translate.endTime,'yyyy-MM-dd');

			var paramsData = {
				startTime:startTime,
				endTime:endTime
			};
			$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
			if($scope.shoumun === 0){
				CoreLoading.setState(true);
				operationAccuracyService.queryOperationAccuracy(paramsData, queryBusinessAccuracy);
			}
		};
		function queryBusinessAccuracy(res){
			var legendName = [];
			var seriesData = [];
			var dateArr = [];
			CoreLoading.setState(false);
			if(res.state === '10000'&&res.data.length>0){
				$scope.flagObj = true;
				var name = [];
				angular.forEach(res.data,function(item){
					if(name.indexOf(item.portName) === -1){
						name.push(item.portName);
					}
				});
				var data = $scope._changeDataState(name,res.data,'portName');
				angular.forEach(data.data,function(item){
					var value = [];
					var percentData = {
						name: item.responseName,
						type: 'bar',
						data: []
					};
					if($scope.translate.staticTypeCode.staticTypeCode === '1'){
						//return;
					}else if($scope.translate.staticTypeCode.staticTypeCode === '2'){
						percentData.type = 'line';
					}else{
						percentData.type = 'scatter';
					}
					legendName.push(item.responseName);

					angular.forEach(item.data,function(data){
						value.push(data.successRate);
            if(dateArr.indexOf(data.time) ===-1){
	            dateArr.push(data.time);
            }
					});
					if($scope.translate.staticTypeCode.staticTypeCode === '3'){
						value = item.data.map(function(per){
							return [per.time,per.successRate];
						});
					}
					percentData.data = value;
					seriesData.push(percentData);
				});
				var option1 = {
					title: {
						text:startTime+"到"+endTime+$scope.translate.sysCode.sysName+legendName.join()+'成功率',
						x: 'center'
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					legend: {
						orient: 'vertical',
						x: 'right',
						y: 'center',
						data: legendName
					},
					grid: {
						x2: '12%'
					},
					xAxis: [
						{
							type: 'category',
							data: dateArr
						}
					],
					yAxis: [
						{
							type: 'value',
							min:80,
							max:100,
							axisLabel : {
								formatter: '{value} %'
							}


						}
					],
					series: seriesData
				};
				myEchars.setOption(option1);
			}
		};
		$timeout(function(){
			$scope.queryBusinessAccuracy();
		});

	}
})();